package org.jeecg.common.util;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

@Slf4j
public class TimingUtils {
    /**
     * 替换消息模板参数生成真正要发送的消息
     * @param text
     * @param map
     * @return
     */
    public static String getTitle(String text, Map<String, String> map){
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String str = "${" + entry.getKey() + "}";
            text = text.replace(str, entry.getValue());
        }
        return text;
    }
    
    /**
     * 获取过期时间
     * @param date
     * @param day
     * @return
     */
    public static Date getExpiredDay(Date date, int day){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
        return calendar.getTime();
    }
    
    public static String getTemporaryToken() {
        RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
        // 模拟登录生成Token
        String token = JwtUtil.sign("admin", "cb362cfeefbf3d8d");
        // 设置Token缓存有效时间为 5 分钟
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 5 * 60 * 1000);
        return token;
    }
}
